// components/head-of/index.js
import api from '../../../api/index';
Component({
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行
      console.log('团长........')

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    teamInfo: {}, //团队信息
    teamOwn: {}, //团长信息
    earnings: {}, //收益信息
    date: '',
    list: [], //收益列表
    isLoading: false,
    isComplete: false,
    pageNo: 1,
    show: false, //展示弹层
    value: '', //公告输入的内容
    showName: false, //修改名字弹层
    showAddress: false, //修改地址弹层
    formData: {
      la: 0,
      lo: 0,
      address: '',
    }, //修改地址内容
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      this.getTeamInfo();
      this.getTeamOwn();
      let date = this.formatTime(new Date());
      this.setData({
        date: date.year + '-' + date.month
      })
      this.getGross();
      this.getEarnings();
    },
    //开放加入
    isOpenJoin() {
      api.isOpenJoin().then(() => {
        wx.showToast({
          title: '操作成功',
        })
        this.setData({
          [`teamOwn.isOpenJoin`]: !this.data.teamOwn.isOpenJoin
        })
      })
    },
    //获取团队收益
    getEarnings() {
      api.getEarnings({
        date: this.data.date
      }).then(res => {
        this.setData({
          list: res.data.data.list
        })
      })
    },
    //修改地址
    updAddress(e) {
      let address = e.detail.value.address;
      this.setData({
        [`formData.address`]: address
      })
      api.updateLocation(this.data.formData).then(() => {
        wx.showToast({
          title: '修改成功',
        })
        this.onShowOrCancelAddress();
        this.setData({
          [`teamInfo.address`]: address
        })
      })
    },
    //选择地址
    chooseAddress() {
     console.log('删除地理位置选择')
    },
    //修改地址弹层
    onShowOrCancelAddress() {
      console.log(111)
      this.setData({
        showAddress: !this.data.showAddress,
      })
    },
    //提交修改团队名字
    submitTeamName(e) {
      let data = e.detail.value;
      api.updateTeamName({
        "brief": data.slogan ? data.slogan : this.data.teamInfo.brief,
        "teamName": data.name ? data.name : this.data.teamInfo.teamName
      }).then(res => {
        wx.showToast({
          title: '修改成功',
        })
        this.getTeamInfo();
        this.onShowOrCancelName();
      })

    },

    //控制修改团队名字的弹层
    onShowOrCancelName() {
      this.setData({
        showName: !this.data.showName,
      })
    },
    //提交公告信息
    submit() {
      api.updateNotice({
        notice: this.data.value
      }).then(() => {
        wx.showToast({
          title: '修改成功',
        })
        this.getTeamInfo();
        this.onShowOrCancel();
      })
    },
    //获取输入的值
    bindinput(e) {
      this.setData({
        value: e.detail.value
      })
    },
    //显示弹层/取消弹层
    onShowOrCancel() {
      this.setData({
        show: !this.data.show,
        value: ''
      })
    },

    //选项时间
    bindDateChange(e) {
      this.setData({
        date: e.detail.value
      })
      this.getGross();
      this.getEarnings();
    },
    //获取收益
    getGross() {
      api.getGross({
        date: this.data.date
      }).then(res => {
        this.setData({
          earnings: res.data.data
        })
      })
    },
    //格式化时间
    formatTime(time) {
      let val = new Date(time);
      let date = {
        year: val.getFullYear(),
        month: val.getMonth() + 1
      }
      return date;
    },
    //获取团队信息
    getTeamInfo() {
      api.getTeamInfo().then(res => {
        this.setData({
          teamInfo: res.data.data
        })
      })
    },
    //获取团长信息
    getTeamOwn() {
      api.getTeamOwn().then(res => {
        res.data.data.rewardText = res.data.data.rewardText ? res.data.data.rewardText.split((/[\n\s+；;]/g)) : [];
        this.setData({
          teamOwn: res.data.data
        })
      })
    },
  }
})